package com.bo.baby2.config;

import com.bo.baby2.entity.TUserAccount;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: baby2_p2p
 * @description: 拦截器配置
 * @author:波
 * @create: 2020-07-13 17:06
 **/
@Component
public class InterceptorConfig implements HandlerInterceptor {
    private static final Logger log=LoggerFactory.getLogger(InterceptorConfig.class);
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String id = request.getSession().getId();
        /*String users = stringRedisTemplate.opsForValue().get(id);*/
        TUserAccount userAccount =(TUserAccount) request.getSession().getAttribute("userAccount");
        String requestURI = request.getRequestURI();
        if (userAccount==null) {
            log.info("拦截请求路径"+requestURI);
            if(requestURI.contains("admin")){
                response.sendRedirect("/admin/login.html");
            }else {
                response.sendRedirect("/login.html");
            }
            return false;
        }
        return true;
    }
}
